//
//  MYTreeTableViewCell.m
//  MYTreeTableView
//
//  Created by mayan on 2018/4/4.
//  Copyright © 2018年 mayan. All rights reserved.
//  https://github.com/mayan29/TreeTableView


#import "MYTreeTableViewCell.h"
#import "MYTreeItem.h"
#import <MJRefresh.h>
#import "NSString+Regex.h"
#import "LKDefineFile.h"
@interface MYTreeTableViewCell ()

@property (nonatomic, strong) MYTreeItem *treeItem;
@property (nonatomic, strong) UIButton *checkButton;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UIImageView *arrowImageView;
@property(nonatomic,strong)UILabel *phoneLabel;
@property(nonatomic,strong)UILabel *levelLabel;
@property(nonatomic,strong)UILabel *priceLabel;
@property(nonatomic,strong)UILabel *idenetLabel;
@property(nonatomic,strong)UILabel *introLabel;
@end

@implementation MYTreeTableViewCell


#pragma mark - Init

+ (instancetype)cellWithTableView:(UITableView *)tableView andTreeItem:(MYTreeItem *)item {
    
    static NSString *ID = @"MYTreeTableViewCell";
    MYTreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[MYTreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.treeItem = item;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.nameLabel.font   = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        self.nameLabel.textColor = COLOR_HEX(0x666666);
        self.indentationWidth = 15;
        self.selectionStyle   = UITableViewCellSelectionStyleNone;
        self.arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 35, 20, 20)];
        [self.contentView addSubview:self.arrowImageView];
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 10, 160, 20)];
        [self.contentView addSubview:_nameLabel];
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 35, 160, 20)];
        _phoneLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        _phoneLabel.textColor = COLOR_HEX(0x666666);
        [self.contentView addSubview:_phoneLabel];
        _levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 60, 160, 20)];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
        _levelLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        _levelLabel.textColor = yellowDefaultColor;
        [self.contentView addSubview:_levelLabel];
        _idenetLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 60, 160, 20)];
        _idenetLabel.textAlignment = NSTextAlignmentCenter;
        _idenetLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        _idenetLabel.textColor = yellowDefaultColor;
        [self.contentView addSubview:_idenetLabel];
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 180, 30, 160, 20)];
        _priceLabel.font = [UIFont fontWithName:@"DINAlternate-Bold" size:21];
        _priceLabel.textColor = COLOR_HEX(0x333333);
        _priceLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_priceLabel];
        _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 180, 60, 160, 20)];
        _introLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        _introLabel.textColor = COLOR_HEX(0x999999);
        _introLabel.textAlignment = NSTextAlignmentRight;
        _introLabel.text = @"月累计收益(元)";
        [self.contentView addSubview:_introLabel];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat minX = 15 + self.indentationLevel * self.indentationWidth;
    
    if (!self.treeItem.isLeaf) {
        CGRect imageViewFrame = self.arrowImageView.frame;
        imageViewFrame.origin.x = minX;
        self.arrowImageView.frame = imageViewFrame;
    }
    
    CGRect textLabelFrame = self.nameLabel.frame;
    textLabelFrame.origin.x = minX + (self.treeItem.isLeaf ? 0 : (self.arrowImageView.bounds.size.width + 2));
    self.nameLabel.frame = textLabelFrame;
    self.phoneLabel.frame = CGRectMake(textLabelFrame.origin.x, self.phoneLabel.mj_y, self.phoneLabel.mj_w, self.phoneLabel.mj_h);
        self.levelLabel.frame = CGRectMake(textLabelFrame.origin.x, 60, [NSString sizeWidthWidth:self.levelLabel.text font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] maxHeight:20].width + 20, 20);
        self.idenetLabel.frame = CGRectMake(textLabelFrame.origin.x + self.levelLabel.mj_w + 10, 60, [NSString sizeWidthWidth:self.idenetLabel.text font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] maxHeight:20].width + 20, 20);
    
}


#pragma mark - Setter

- (void)setTreeItem:(MYTreeItem *)treeItem {
    self.idenetLabel.hidden = NO;
    self.levelLabel.hidden = NO;
    _treeItem = treeItem;
    NSDictionary *treeDic = treeItem.data;
    self.indentationLevel = treeItem.level;
    self.nameLabel.text   = treeItem.name;
    self.phoneLabel.text = treeDic[@"user_phone"];
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f",[treeDic[@"price"] floatValue]];
    self.levelLabel.text = [NSString backSuboradinateTitleAccoringType:[NSString stringWithFormat:@"%@",treeDic[@"level"]]];
    self.levelLabel.mj_w = [NSString sizeWidthWidth:self.levelLabel.text font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] maxHeight:20].width + 20;
    self.levelLabel.layer.borderWidth = 1;
    self.levelLabel.layer.borderColor = yellowDefaultColor.CGColor;
    self.levelLabel.layer.cornerRadius = 10;
    self.levelLabel.layer.masksToBounds = YES;
    if (self.levelLabel.text.length == 0) {
        self.levelLabel.hidden = YES;
    }
    self.idenetLabel.frame = CGRectMake(42 + self.levelLabel.mj_w + 10, 60, 0, 20);
    if ([NSString stringWithFormat:@"%@",treeDic[@"identi"]].length == 0) {
        self.idenetLabel.hidden = YES;
    }else{
        if ([[NSString stringWithFormat:@"%@",treeDic[@"identi"]] isEqualToString:@"1"]) {
            self.idenetLabel.text = @"内部";
        }else{
            self.idenetLabel.text = @"外部";
        }
    }
    self.idenetLabel.mj_w = [NSString sizeWidthWidth:self.idenetLabel.text font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] maxHeight:20].width + 20;
    self.idenetLabel.layer.borderWidth = 1;
    self.idenetLabel.layer.borderColor = yellowDefaultColor.CGColor;
    self.idenetLabel.layer.cornerRadius = 10;
    self.idenetLabel.layer.masksToBounds = YES;
    self.arrowImageView.image  = treeItem.isLeaf ? nil : [UIImage imageNamed:@"MYTreeTableView.bundle/arrow"];
    self.accessoryView    = self.checkButton;
   
    [self refreshArrow];
    [self.checkButton setImage:[self getCheckImage] forState:UIControlStateNormal];
}

- (void)setIsShowArrow:(BOOL)isShowArrow {
    _isShowArrow = isShowArrow;
    
    if (!isShowArrow && self.arrowImageView.image) {
        self.arrowImageView.image = nil;
    }
}

- (void)setIsShowCheck:(BOOL)isShowCheck {
    _isShowCheck = isShowCheck;
    
    if (!isShowCheck && self.accessoryView) {
        self.accessoryView = nil;
    }
    if (_isShowCheck) {
        _priceLabel.hidden = YES;
        _introLabel.hidden = YES;
    }
}


#pragma mark - Public Method

- (void)updateItem {
    // 刷新 title 前面的箭头方向
    [UIView animateWithDuration:0.25 animations:^{
        [self refreshArrow];
    }];
}


#pragma mark - Lazy Load

- (UIButton *)checkButton {
    if (!_checkButton) {
        
        UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkButton addTarget:self action:@selector(checkButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [checkButton setImage:[self getCheckImage] forState:UIControlStateNormal];
        checkButton.adjustsImageWhenHighlighted = NO;
        checkButton.frame = CGRectMake(0, 0, self.contentView.bounds.size.height, self.contentView.bounds.size.height);
        CGFloat aEdgeInset = (checkButton.frame.size.height - checkButton.imageView.image.size.height) / 2;
        checkButton.contentEdgeInsets = UIEdgeInsetsMake(aEdgeInset, aEdgeInset, aEdgeInset, aEdgeInset);
        
        _checkButton = checkButton;
    }
    return _checkButton;
}


#pragma mark - Private Method

- (void)refreshArrow {
    
    if (self.treeItem.isExpand) {
        self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.arrowImageView.transform = CGAffineTransformMakeRotation(0);
    }
}

- (void)checkButtonClick:(UIButton *)sender {
    if (self.checkButtonClickBlock) {
        self.checkButtonClickBlock(self.treeItem);
    }
}

- (UIImage *)getCheckImage {

    switch (self.treeItem.checkState) {
        case MYTreeItemDefault:
            return [UIImage imageNamed:@"MYTreeTableView.bundle/checkbox-uncheck"];
            break;
        case MYTreeItemChecked:
            return [UIImage imageNamed:@"MYTreeTableView.bundle/checkbox-checked"];
            break;
        case MYTreeItemHalfChecked:
            return [UIImage imageNamed:@"MYTreeTableView.bundle/checkbox-partial"];
            break;
        default:
            return nil;
            break;
    }
}


@end
